#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
#include<Windows.h>
typedef struct generatii{
	char nume[10];
	char prenume[10];
	char d_nasterii[11];
	generatii *next;
}gen;
typedef struct vlist{
	int generatie;
	gen * head;
	vlist * next;
	vlist * prev;
}vlist;
vlist * head=NULL;
int genmax=0;
bool cautare_t_proprietati(int g,char nume[],char pnume[],vlist * nod)
{
	vlist * crt=nod;
	gen * last;
	bool ok=false;
	int index=g/2;
	while((crt->generatie>=index)&&(crt->generatie>=0)&&(crt!=NULL))
	{
		last=crt->head;
		while((last!=NULL)&&(ok==false))
		{
			if((stricmp(nume,last->nume)==0)&&(stricmp(pnume,last->prenume)==0))
			{
				printf("Persoana cautata face parte din generatia %d si are urmatoarele date :\n %s  %s  %s\n",crt->generatie,last->nume,last->prenume,last->d_nasterii);
				ok=true;
				return ok;
			}
			last=last->next;
		}
		crt=crt->prev;
	}
	if(ok==false)
	{
		ok=cautare_t_proprietati(index,nume,pnume,crt);
	}
	return ok;
}

void inserare_date()
{
	char * nume=(char *)malloc(sizeof(char)),* prenume=(char *)malloc(sizeof(char)),* data=(char *)malloc(sizeof(char));
	printf("Dati numele persoanei: ");
	scanf("%s",nume);
	printf("Dati prenumele persoanei: ");
	scanf("%s",prenume);
	printf("Dati data nasterii persoanei: ");
	scanf("%s",data);
	vlist *elem=head;
	while(elem->prev!=NULL)
	{
		elem=elem->prev;
	}
	if(elem->generatie!=0)
	{
	elem->prev=(vlist *) malloc(sizeof(vlist));
	elem->prev->generatie=0;
	elem->prev->head=(gen *)malloc(sizeof(gen));
	gen * el=elem->prev->head;
	strcpy(el->nume,nume);
	strcpy(el->prenume,prenume);
	strcpy(el->d_nasterii,data);
	el->next=NULL;
	elem->prev->prev=NULL;
	genmax++;
	}
	else
	{
		gen * last=elem->prev->head;
		while(last->next!=NULL)
		{
			last=last->next;
		}
		last->next=(gen *)malloc(sizeof(gen));
		strcpy(last->next->nume,nume);
		strcpy(last->next->prenume,prenume);
		strcpy(last->next->d_nasterii,data);
		last->next->next=NULL;
	}
}
void inserare( int g, char nume[],char pnume[],char* data)
{
	vlist * elem=(vlist *) malloc(sizeof(vlist));
	elem->generatie=g;
	elem->head=NULL;
	elem->next=NULL;
	elem->prev=NULL;
	if(head==NULL)
	{
		genmax++;
		head=elem;
		head->head=(gen *)malloc(sizeof(gen));
		strcpy(head->head->nume,nume);
		strcpy(head->head->prenume,pnume);
		strcpy(head->head->d_nasterii,data);
		head->head->next=NULL;
	}
	else
	{
		gen * el=(gen *)malloc(sizeof(gen));
		strcpy(el->nume,nume);
		strcpy(el->prenume,pnume);
		strcpy(el->d_nasterii,data);
		el->next=NULL;
		vlist * search=head,*aux;
		int ok=1,index=search->generatie;
		while((search!=NULL)&&(ok==1)&&(search->generatie<=g))
		{
			if(search->generatie==g)
			{
				ok=0;
			}
			aux=search;
			search=search->next;
		}
		if(ok==0)
		{
			if(aux->head==NULL)
			{
				aux->head=el;
			}
			else
			{
				gen * last=aux->head;
				while(last->next!=NULL)
				{
					last=last->next;
				}
				last->next=el;
			}
		}
		else
		{
			if(search==NULL)
			{
				genmax++;
				search=aux;
				aux->next=elem;
				elem->prev=search;
				elem->head=(gen *) malloc(sizeof(gen));
				elem->head=el;
			}
			else
			{	
				search=aux;
				elem->next=search->next;
				search->next=elem;
				elem->prev=search;
				elem->next->prev=elem;
				elem->head=(gen *)malloc(sizeof(gen));
				elem->head=el;
			}
		}
	}				
}
void afisare()
{
	gen * el;
	vlist * elem=head;
	if(head==NULL)
	{
		printf("lista este goala");
	}
	else
	{
		while(elem!=NULL)
		{
			el=elem->head;
			printf("\n\nGeneratia %d:\n",elem->generatie);
			while(el!=NULL)
			{
				printf("%s	%s		%s\n",el->nume,el->prenume,el->d_nasterii);
				el=el->next;
			}
			elem=elem->prev;
		} 
	}
}

void main()
{
	
	FILE *f=fopen("baza_date.txt","r");
	char s[100], nume[10],prenume[10],i,*data,*p,*c;
	int index;
	if(f==NULL)
	{
		printf("Eroare la deschiderea fisierului!!!");
		return;
	}
	data=(char *)malloc(sizeof(char));
	c=fgets(s,100,f);
	while(c)
	{
		s[strlen(s)-1]='\0';
		p=strtok(s," ");
		i=atoi(p);
		p=strtok(NULL," ");
		strcpy(nume,p);
		p=strtok(NULL," ");
		strcpy(prenume,p);
		p=strtok(NULL," \0");
		strcpy(data,p);
		inserare(i,nume,prenume,data);
		c=fgets(s,100,f);
	}
	vlist * el=head;
	while(el->next!=NULL)
	{
		el=el->next;
	}
	head=el;
	afisare();
	inserare_date();
	afisare();
	printf("\n\n\n\n\n %d\n",genmax);
	printf("Dati numele persoanei cautate:");
	scanf("%s",nume);
	printf("Dati prenumele persoanei cautate:");
	scanf("%s",prenume);
	bool ok;
	ok=cautare_t_proprietati(genmax,nume,prenume,head);
	if(ok==false)
	{
		printf("Individul lipseste cu desavarsire! ");
	}
	getch();
}